package com.sprd.simple.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.util.Log;

import com.sprd.simple.launcher.Launcher;
import com.sprd.simple.launcher.R;
import com.sprd.simple.model.IconInfo;
import com.sprd.simple.model.TextSpeech;

import java.util.List;
import java.util.ArrayList;
import com.sprd.simple.util.HomeConstants;
import java.util.HashSet;
import java.util.Set;
import android.content.SharedPreferences;
import android.text.TextUtils;
/**
 * Created by SPDR on 2016/10/25.
 */

public abstract class BaseGridAdapter extends BaseAdapter {

    private List<IconInfo> mArrayList = new ArrayList<>();
    private Context mContext = null;
    private String[] appNameForTTS;
    private GridView mGridView = null;
    protected int mPosition = 0;

    public BaseGridAdapter(List<IconInfo> arrayList, Context context, String[] appName, GridView gridView) {
        mArrayList = arrayList;
        mContext = context;
        appNameForTTS = appName;
        mGridView = gridView;
        saveDesktopAppInfo(context, arrayList);
    }

    /**
     * write desktop apk info to sharedPreferences
     *
     * @param context
     * @param iconInfos
     */
    private void saveDesktopAppInfo(Context context, List<IconInfo> iconInfos) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(HomeConstants.SP_NAME,
                Context.MODE_PRIVATE);
        Set<String> packageNameSet = sharedPreferences.getStringSet(HomeConstants.HOME_APPS_NAMES, null);
        if (packageNameSet == null) {
            packageNameSet = new HashSet<>();
        }
        for (IconInfo info : iconInfos) {
            String packageName = info != null ? info.getPackageName() : "";
            if (!TextUtils.isEmpty(packageName)) {
                packageNameSet.add(packageName);
            }
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putStringSet(HomeConstants.HOME_APPS_NAMES, packageNameSet);
        editor.commit();
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public IconInfo getItem(int position) {
        return mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup parent);

    public int getItemHeight() {
        int count = mGridView.getCount();
        int numColumns = 0;
        numColumns = mGridView.getNumColumns();

        float marginSize = mContext.getResources().getDimension(R.dimen.layout_margin_size);
        float horizontalSpacing = mContext.getResources().getDimension(R.dimen.horizontalSpacing_grid_view);
        int gridHeight = mGridView.getHeight();
        int row = count / numColumns;
        int itemHeight = (int) ((gridHeight - horizontalSpacing * (row - 1)) / (row));
        return itemHeight;
    }

    public void setSelectPosition(int position) {
        mPosition = position;
    }
    public int getSelectPosition() {
        return mPosition;
    }
}
